Use g_file_set_contents(). (#308722, Morten Welinder)
authorMatthias Clasen <mclasen@redhat.com>
Thu, 23 Jun 2005 04:14:17 +0000 (04:14 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Thu, 23 Jun 2005 04:14:17 +0000 (04:14 +0000)
2005-06-23  Matthias Clasen  <mclasen@redhat.com>

* gtk/gtkfilesystemunix.c (bookmark_list_write): Use
g_file_set_contents().  (#308722, Morten Welinder)

ChangeLog
ChangeLog.pre-2-10
ChangeLog.pre-2-8
gtk/gtkfilesystemunix.c

index d611879d2e4caf9bd8375229c30184e1a6bedca8..5ef1363f072dff7150348388ab4716cc95ba353a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2005-06-23  Matthias Clasen  <mclasen@redhat.com>
+
+       * gtk/gtkfilesystemunix.c (bookmark_list_write): Use
+       g_file_set_contents().  (#308722, Morten Welinder)
+
 2005-06-22  Matthias Clasen  <mclasen@redhat.com>
 
        * gdk/x11/gdkdnd-x11.c (xdnd_finished_filter): Set the
index d611879d2e4caf9bd8375229c30184e1a6bedca8..5ef1363f072dff7150348388ab4716cc95ba353a 100644 (file)
@@ -1,3 +1,8 @@
+2005-06-23  Matthias Clasen  <mclasen@redhat.com>
+
+       * gtk/gtkfilesystemunix.c (bookmark_list_write): Use
+       g_file_set_contents().  (#308722, Morten Welinder)
+
 2005-06-22  Matthias Clasen  <mclasen@redhat.com>
 
        * gdk/x11/gdkdnd-x11.c (xdnd_finished_filter): Set the
index d611879d2e4caf9bd8375229c30184e1a6bedca8..5ef1363f072dff7150348388ab4716cc95ba353a 100644 (file)
@@ -1,3 +1,8 @@
+2005-06-23  Matthias Clasen  <mclasen@redhat.com>
+
+       * gtk/gtkfilesystemunix.c (bookmark_list_write): Use
+       g_file_set_contents().  (#308722, Morten Welinder)
+
 2005-06-22  Matthias Clasen  <mclasen@redhat.com>
 
        * gdk/x11/gdkdnd-x11.c (xdnd_finished_filter): Set the
index 16928261449f39520fee90dbb5fdbfa6c0de37be..097b9a82f32fcc9d9b185e343df8a524b2e12aed 100644 (file)
@@ -42,7 +42,6 @@
 #include <time.h>
 
 #define BOOKMARKS_FILENAME ".gtk-bookmarks"
-#define BOOKMARKS_TMP_FILENAME ".gtk-bookmarks-XXXXXX"
 
 #define HIDDEN_FILENAME ".hidden"
 
@@ -1377,13 +1376,12 @@ is_local_uri (const char *uri)
 }
 
 static char *
-bookmark_get_filename (gboolean tmp_file)
+bookmark_get_filename (void)
 {
   char *filename;
 
   filename = g_build_filename (g_get_home_dir (), 
-                              tmp_file ? BOOKMARKS_TMP_FILENAME : BOOKMARKS_FILENAME, 
-                              NULL);
+                              BOOKMARKS_FILENAME, NULL);
   g_assert (filename != NULL);
   return filename;
 }
@@ -1395,7 +1393,7 @@ bookmark_list_read (GSList **bookmarks, GError **error)
   gchar *contents;
   gboolean result = FALSE;
 
-  filename = bookmark_get_filename (FALSE);
+  filename = bookmark_get_filename ();
   *bookmarks = NULL;
 
   if (g_file_get_contents (filename, &contents, NULL, error))
@@ -1429,79 +1427,41 @@ bookmark_list_read (GSList **bookmarks, GError **error)
 }
 
 static gboolean
-bookmark_list_write (GSList *bookmarks, GError **error)
+bookmark_list_write (GSList  *bookmarks, 
+                    GError **error)
 {
-  char *tmp_filename;
+  GSList *l;
+  GString *string;
   char *filename;
-  gboolean result = TRUE;
-  FILE *file;
-  int fd;
-  int saved_errno;
-
-  /* First, write a temporary file */
+  GError *tmp_error = NULL;
+  gboolean result;
 
-  tmp_filename = bookmark_get_filename (TRUE);
-  filename = bookmark_get_filename (FALSE);
+  string = g_string_new ("");
 
-  fd = g_mkstemp (tmp_filename);
-  if (fd == -1)
+  for (l = bookmarks; l; l = l->next)
     {
-      saved_errno = errno;
-      goto io_error;
+      g_string_append (string, l->data);
+      g_string_append_c (string, '\n');
     }
 
-  if ((file = fdopen (fd, "w")) != NULL)
-    {
-      GSList *l;
-
-      for (l = bookmarks; l; l = l->next)
-       if (fputs (l->data, file) == EOF
-           || fputs ("\n", file) == EOF)
-         {
-           saved_errno = errno;
-           goto io_error;
-         }
+  filename = bookmark_get_filename ();
 
-      if (fclose (file) == EOF)
-       {
-         saved_errno = errno;
-         goto io_error;
-       }
+  result = g_file_set_contents (filename, string->str, -1, &tmp_error);
 
-      if (rename (tmp_filename, filename) == -1)
-       {
-         saved_errno = errno;
-         goto io_error;
-       }
+  g_free (filename);
+  g_string_free (string, TRUE);
 
-      result = TRUE;
-      goto out;
-    }
-  else
+  if (!result)
     {
-      saved_errno = errno;
-
-      /* fdopen() failed, so we can't do much error checking here anyway */
-      close (fd);
+      g_set_error (error,
+                  GTK_FILE_SYSTEM_ERROR,
+                  GTK_FILE_SYSTEM_ERROR_FAILED,
+                  _("Bookmark saving failed: %s"),
+                  tmp_error->message);
+      
+      g_error_free (tmp_error);
     }
 
- io_error:
-
-  g_set_error (error,
-              GTK_FILE_SYSTEM_ERROR,
-              GTK_FILE_SYSTEM_ERROR_FAILED,
-              _("Bookmark saving failed: %s"),
-              g_strerror (saved_errno));
-  result = FALSE;
-
-  if (fd != -1)
-    unlink (tmp_filename); /* again, not much error checking we can do here */
-
- out:
-
-  g_free (filename);
-  g_free (tmp_filename);
-
   return result;
 }